package com.fintech.datagroud.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fintech.datagroud.dao.shzx.IntShContactMapper;
import com.fintech.datagroud.dao.shzx.IntShGuaranteeRecordsMapper;
import com.fintech.datagroud.dao.shzx.IntShLiveAddressMapper;
import com.fintech.datagroud.dao.shzx.IntShLoanApplyInfoMapper;
import com.fintech.datagroud.dao.shzx.IntShLoanRecordsMapper;
import com.fintech.datagroud.dao.shzx.IntShReportInfoMapper;
import com.fintech.datagroud.dao.shzx.IntShWorkInfoMapper;
import com.fintech.datagroud.entity.shzx.IntShContact;
import com.fintech.datagroud.entity.shzx.IntShGuaranteeRecords;
import com.fintech.datagroud.entity.shzx.IntShLiveAddress;
import com.fintech.datagroud.entity.shzx.IntShLoanApplyInfo;
import com.fintech.datagroud.entity.shzx.IntShLoanRecords;
import com.fintech.datagroud.entity.shzx.IntShReportInfo;
import com.fintech.datagroud.entity.shzx.IntShWorkInfo;
import com.fintech.datagroud.service.DGShzxService;
import com.fintech.third.model.shzx.Address;
import com.fintech.third.model.shzx.ContactInfo;
import com.fintech.third.model.shzx.ContactInfoSecond;
import com.fintech.third.model.shzx.CreditHeader;
import com.fintech.third.model.shzx.CreditReport;
import com.fintech.third.model.shzx.GuaranteeInfo;
import com.fintech.third.model.shzx.Info;
import com.fintech.third.model.shzx.Loan;
import com.fintech.third.model.shzx.LoanApplyInfo;
import com.fintech.third.model.shzx.LoanTxnInfo;
import com.fintech.third.model.shzx.PersonalIDInfo;
import com.fintech.third.model.shzx.SpecialTxnInfo;
import com.fintech.third.model.shzx.WarrantyInformationRecord;
import com.fintech.third.model.shzx.Workplace;
import com.fintech.third.utils.DateUtil;


/**
 * 类说明：上海资信服务实现类
 * @date 2017年2月17日
 * @author SHAOYUN
 * 
 **/
@Service("dGShzxService")
public class DGShzxServiceImpl implements DGShzxService {
	
	@Autowired
	IntShReportInfoMapper reportInfoMapper;
	@Autowired
	IntShLiveAddressMapper addressMapper;
	@Autowired
	IntShWorkInfoMapper workInfoMapper;
	@Autowired
	IntShContactMapper contactMapper;
	@Autowired
	IntShLoanRecordsMapper loanRecordsMapper;
	@Autowired
	IntShLoanApplyInfoMapper loanApplyInfoMapper;
	@Autowired
	IntShGuaranteeRecordsMapper guaranteeRecordsMapper;
	
	@Override
	public boolean creditReport(CreditReport creditReport,String serialNum,String loanId,String certno) {
		
		IntShReportInfo reportInfo = new IntShReportInfo();
		
		// 信用报告头
		CreditHeader creditHeader = creditReport.getCreditHeader();
		reportInfo.setCustIdNumber(certno);
		reportInfo.setSerialNum(serialNum);
		reportInfo.setLoanId(loanId);
		reportInfo.setCreatedBy("sys");
		reportInfo.setCreatedDate(new Date());
		reportInfo.setUpdatedBy("sys");
		reportInfo.setUpdatedDate(new Date());
		if(creditHeader != null){
			reportInfo.setReportNo(creditHeader.getReportNo());
			reportInfo.setQueryReason(creditHeader.getQueryReason());
			reportInfo.setReportTime(DateUtil.strToDate2(creditHeader.getReportTime().replace(".","-")));
		}
		
		// 个人基本信息
		PersonalIDInfo personalIDInfo = creditReport.getPersonalIDInfo();
		List<Address> addressList = new ArrayList<>();
		List<Workplace> workplaceList = new ArrayList<>();
		List<ContactInfo> contactInfoList = new ArrayList<>();
		List<ContactInfoSecond> contactInfoSecondList = new ArrayList<>();
		List<LoanApplyInfo> loanApplyInfoList = new ArrayList<>();
		
		if(personalIDInfo != null){
			reportInfo.setName(personalIDInfo.getName());
			reportInfo.setCertType(personalIDInfo.getCertType());
			reportInfo.setCertNo(personalIDInfo.getCertNo());
			reportInfo.setGender(personalIDInfo.getGender());
			reportInfo.setBirthDate(DateUtil.strToDate2(personalIDInfo.getBirthDate().replace(".","-")));
			reportInfo.setMarrialStatus(personalIDInfo.getMaritalStatus().getStatus());
			reportInfo.setMarrialInfoDate(DateUtil.strToDate2(personalIDInfo.getMaritalStatus().getInfoDate().replace(".","-")));
			reportInfo.setEduLevel(personalIDInfo.getEduLevel().getLevel());
			reportInfo.setEduInfoDate(DateUtil.strToDate2(personalIDInfo.getEduLevel().getInfoDate().replace(".","-")));
			reportInfo.setWorkTitle(personalIDInfo.getWorkTitle().getTitle());
			reportInfo.setWorkTitleInfoDate(DateUtil.strToDate2(personalIDInfo.getWorkTitle().getInfoDate().replace(".","-")));
			reportInfo.setHomePhone(personalIDInfo.getHomePhone().getPhone());
			reportInfo.setHomePhoneInfoDate(DateUtil.strToDate2(personalIDInfo.getEmail().getInfoDate().replace(".","-")));
			reportInfo.setMobilePhone(personalIDInfo.getMobilePhone().getPhone());
			reportInfo.setMobilePhoneInfoDate(DateUtil.strToDate2(personalIDInfo.getMobilePhone().getInfoDate().replace(".","-")));
			reportInfo.setEmail(personalIDInfo.getEmail().getEmail());
			reportInfo.setEmailInfoDate(DateUtil.strToDate2(personalIDInfo.getEmail().getInfoDate().replace(".","-")));
			// 配偶基本信息
			reportInfo.setSpouse(personalIDInfo.getSpouse());
			reportInfo.setScertType(personalIDInfo.getsCertType());
			reportInfo.setScertNo(personalIDInfo.getsCertNo());
			reportInfo.setSgender(personalIDInfo.getsGender());
			reportInfo.setSbirthDate(personalIDInfo.getsBirthDate());
			reportInfo.setSpouseWorkPlace(personalIDInfo.getSpouseWorkplace().getPlace());
			reportInfo.setSpouseWorkPlaceInfoDate(DateUtil.strToDate2(personalIDInfo.getSpouseWorkplace().getInfoDate().replace(".","-")));
			reportInfo.setSpousePhone(personalIDInfo.getSpousePhone().getPhone());
			reportInfo.setSpousePhoneInfoDate(DateUtil.strToDate2(personalIDInfo.getSpousePhone().getInfoDate().replace(".","-")));
			// 地址信息和工作单位信息
			addressList = personalIDInfo.getAddressList();
			workplaceList = personalIDInfo.getWorkplaceList();
			contactInfoList = personalIDInfo.getContactInfoList();
			contactInfoSecondList = personalIDInfo.getContactInfoListSecond();
			saveContactInfoSecond(reportInfo.getReportNo(),contactInfoSecondList,serialNum,loanId,certno);
			saveContactInfo(reportInfo.getReportNo(),contactInfoList,serialNum,loanId,certno);
			saveWorkplace(reportInfo.getReportNo(),workplaceList,serialNum,loanId,certno);
			saveAddress(reportInfo.getReportNo(),addressList,serialNum,loanId,certno);
		}
		
		LoanTxnInfo loanTxnInfo = creditReport.getLoanTxnInfo();
		if (loanTxnInfo != null) {
			// 贷款人基本信息
			Info info = loanTxnInfo.getInfo();
			if (info != null) {
				reportInfo.setLoanCount(Integer.getInteger(info.getLoanCount()));
				reportInfo.setFirstLoanDate(DateUtil.strToDate2(info.getFirstLoanDate().replace(".","-")));
				reportInfo.setMaxCreditLine(new BigDecimal(info.getMaxCreditLine()));
				reportInfo.setTotalLoanAmt(new BigDecimal(info.getTotalLoanAmt()));
				reportInfo.setLoanBalance(new BigDecimal(info.getLoanBalance()));
				reportInfo.setPlanMonthPay(new BigDecimal(info.getPlanMonthPay()));
				reportInfo.setCurrOverdueTotalAmt(new BigDecimal(info.getCurrOverdueTotalAmt()));
				reportInfo.setMaxOverdueAmt(new BigDecimal(info.getMaxOverdueAmt()));
				reportInfo.setMaxOverdueTenor(Integer.getInteger(info.getMaxOverdueTenor()));
			}
			// 贷款信息
			saveLoanRecord(reportInfo.getReportNo(),loanTxnInfo.getLoan(),serialNum,loanId,certno);
			
		}
		
		// 客户信息存库
		reportInfoMapper.insert(reportInfo);
		// 保存贷款申请信息
		saveLoanApplyInfo(reportInfo.getReportNo(),creditReport.getLoanApplyInfo(),serialNum,loanId,certno);
		// 保存为他人担保信息
		saveGuaranteeInfo(reportInfo.getReportNo(),creditReport.getGuaranteeInfo(),serialNum,loanId,certno);
		// 特殊交易信息
		saveSpecialTxnInfo(reportInfo.getReportNo(),creditReport.getSpecialTxnInfo(),serialNum,loanId,certno);
		return false;
	}
	

	private void saveSpecialTxnInfo(String reportNo, SpecialTxnInfo specialTxnInfo,String serialNum,String loanId,String certno) {
		if(specialTxnInfo != null){
			
		}
	}


	private void saveGuaranteeInfo(String reportNo, GuaranteeInfo guaranteeInfo,String serialNum,String loanId,String certno) {
		if(guaranteeInfo != null){
			List<WarrantyInformationRecord> WarrantyInformationRecordList = guaranteeInfo.getWarrantyInformationRecord();
			if(WarrantyInformationRecordList!=null && WarrantyInformationRecordList.size()>0){
				for (WarrantyInformationRecord warrantyInfo:WarrantyInformationRecordList) {
					IntShGuaranteeRecords guaranteeRecords = new IntShGuaranteeRecords();
					guaranteeRecords.setReportNo(reportNo);
					guaranteeRecords.setGuaranteeItem(warrantyInfo.getGuaranteeItem());
					guaranteeRecords.setGuaranteeAmount(new BigDecimal(warrantyInfo.getGuaranteeAmount()));
					guaranteeRecords.setGuaranteeDate(DateUtil.strToDate2(warrantyInfo.getGuaranteeDate().replace(".","-")));
					guaranteeRecords.setGuaranteeRelationship(warrantyInfo.getGuaranteeRelationship());
					guaranteeRecords.setInfoDate(DateUtil.strToDate2(warrantyInfo.getDateOfInformationAcquisition().replace(".","-")));
					guaranteeRecords.setCustIdNumber(certno);
					guaranteeRecords.setSerialNum(serialNum);
					guaranteeRecords.setLoanId(loanId);
					guaranteeRecords.setCreatedBy("sys");
					guaranteeRecords.setCreatedDate(new Date());
					guaranteeRecords.setUpdatedBy("sys");
					guaranteeRecords.setUpdatedDate(new Date());
					guaranteeRecordsMapper.insert(guaranteeRecords);
				}
			}
			
			
		}
		
	}

	void saveLoanApplyInfo(String reportNo,List<LoanApplyInfo> loanApplyInfoList,String serialNum,String loanId,String certno){
		if(loanApplyInfoList!=null && loanApplyInfoList.size()>0){
			for(LoanApplyInfo loanApplyInfo:loanApplyInfoList){
				if(loanApplyInfo!=null){
				IntShLoanApplyInfo applyInfo = new IntShLoanApplyInfo();
				applyInfo.setApplyNo(applyInfo.getApplyNo());
				applyInfo.setReportNo(reportNo);
				applyInfo.setApplyOrg(loanApplyInfo.getApplicationBody());
				applyInfo.setApplyDate(DateUtil.strToDate2(loanApplyInfo.getApplicationTime().replace(".","-")));
				applyInfo.setApplyAmount(new BigDecimal(loanApplyInfo.getApplicationAmount()));
				applyInfo.setApplyTenor(Integer.getInteger(loanApplyInfo.getNumberOfApplications()));
				applyInfo.setApplyType(loanApplyInfo.getApplicationType());
				applyInfo.setCustIdNumber(certno);
				applyInfo.setSerialNum(serialNum);
				applyInfo.setLoanId(loanId);
				applyInfo.setCreatedBy("sys");
				applyInfo.setCreatedDate(new Date());
				applyInfo.setUpdatedBy("sys");
				applyInfo.setUpdatedDate(new Date());
				loanApplyInfoMapper.insert(applyInfo);
				}
			}
		}
	}
	
	
	// 地址信息存库
	void saveAddress(String reportNo,List<Address> addressList,String serialNum,String loanId,String certno){
		if(addressList!=null && addressList.size()>0){
			for(Address address: addressList){
				
				IntShLiveAddress liveAddress = new IntShLiveAddress();
				liveAddress.setReportNo(reportNo);
				liveAddress.setAddressNo(address.getNo());
				liveAddress.setAddressDetail(address.getAddress());
				liveAddress.setAddressInfoDate(DateUtil.strToDate2(address.getInfoDate().replace(".","-")));
				// TODO 设置地址表中的查询流水号
				liveAddress.setSerialNum(serialNum);
				liveAddress.setCustIdNumber(certno);
				liveAddress.setLoanId(loanId);
				liveAddress.setCreatedBy("sys");
				liveAddress.setCreatedDate(new Date());
				liveAddress.setUpdatedBy("sys");
				liveAddress.setUpdatedDate(new Date());
				// 保存地址表
				addressMapper.insert(liveAddress);
			}
		}
	}
	
	
	// 工作单位存库
	void saveWorkplace(String reportNo,List<Workplace> workplaceList,String serialNum,String loanId,String certno){
		if(workplaceList!=null && workplaceList.size()>0){
			for(Workplace workplace: workplaceList){
				
				IntShWorkInfo workInfo = new IntShWorkInfo();
				workInfo.setReportNo(reportNo);
				workInfo.setWorkNo(workplace.getNo());
				workInfo.setWorkUnit(workplace.getPlace());
				workInfo.setJobType(workplace.getJob());
				workInfo.setInfoDate(DateUtil.strToDate2(workplace.getInfoDate().replace(".","-")));
				// TODO 设置工作信息表中的查询流水号
				workInfo.setSerialNum(serialNum);
				workInfo.setCustIdNumber(certno);
				workInfo.setLoanId(loanId);
				workInfo.setCreatedBy("sys");
				workInfo.setCreatedDate(new Date());
				workInfo.setUpdatedBy("sys");
				workInfo.setUpdatedDate(new Date());
				// 保存工作信息表
				workInfoMapper.insert(workInfo);
			}
		}
	}
	
	
	// 第一联系人存库
	void saveContactInfo(String reportNo,List<ContactInfo> contactInfoList,String serialNum,String loanId,String certno){
		if(contactInfoList!=null && contactInfoList.size()>0){
			for(ContactInfo contactInfo: contactInfoList){
				
				IntShContact contact = new IntShContact();
				contact.setReportNo(reportNo);
				contact.setName(contactInfo.getName());
				contact.setRelation(contactInfo.getRelation());
				contact.setPhone(contactInfo.getPhone());
				contact.setInfoDate(DateUtil.strToDate2(contactInfo.getInfoDate().replace(".","-")));
				contact.setType("01");
				// TODO 设置联系人信息表中的查询流水号
				contact.setSerialNum(serialNum);
				contact.setCustIdNumber(certno);
				contact.setLoanId(loanId);
				contact.setCreatedBy("sys");
				contact.setCreatedDate(new Date());
				contact.setUpdatedBy("sys");
				contact.setUpdatedDate(new Date());
				// 保存联系人信息表
				contactMapper.insert(contact);
			}
		}
	}
	
	
	// 第二联系人存库
	void saveContactInfoSecond(String reportNo,List<ContactInfoSecond> contactInfoSecondList,String serialNum,String loanId,String certno){

		if(contactInfoSecondList!=null && contactInfoSecondList.size()>0){
			
			for(ContactInfoSecond contactInfo: contactInfoSecondList){
				IntShContact contact = new IntShContact();
				contact.setReportNo(reportNo);
				contact.setName(contactInfo.getName());
				contact.setRelation(contactInfo.getRelation());
				contact.setPhone(contactInfo.getPhone());
				contact.setInfoDate(DateUtil.strToDate2(contactInfo.getInfoDate().replace(".","-")));
				contact.setType("02");
				// TODO 设置联系人信息表中的查询流水号
				contact.setSerialNum(serialNum);
				contact.setCustIdNumber(certno);
				contact.setLoanId(loanId);
				contact.setCreatedBy("sys");
				contact.setCreatedDate(new Date());
				contact.setUpdatedBy("sys");
				contact.setUpdatedDate(new Date());
				// 保存联系人信息表
				contactMapper.insert(contact);
			}
		}	
	}
	
	// 贷款信息存库
	void saveLoanRecord(String reportNo,Loan loan,String serialNum,String loanId,String certno){
		if (loan != null) {
			IntShLoanRecords loanRecord = new IntShLoanRecords();
			loanRecord.setReportNo(reportNo);
			// TODO 贷款表序号
			loanRecord.setReportNo("");
			loanRecord.setLoanProject(loan.getLoanProject());
			loanRecord.setOrgName(loan.getOrgName());
			loanRecord.setCreditLine(new BigDecimal(loan.getCreditLine()));
			loanRecord.setGuaranteeMethod(loan.getGuaranteeMethod());
			loanRecord.setOpenDate(DateUtil.strToDate2(loan.getOpenDate().replace(".","-")));
			loanRecord.setCurrency(loan.getCurrency());
			loanRecord.setPlace(loan.getPlace());
			loanRecord.setShareCreditLine(new BigDecimal(loan.getShareCreditLine()));
			loanRecord.setMaxDebt(new BigDecimal(loan.getMaxDebt()));
			loanRecord.setRepayFrequency(loan.getRepayFrequency());
			loanRecord.setFinalLoanBalance(new BigDecimal(loan.getFinalLoanBalance()));
			loanRecord.setRemainRepayMonth(Integer.getInteger(loan.getRemainRepayMonth()));
			loanRecord.setCurrRepayDate(DateUtil.strToDate2(loan.getCurrRepayDate().replace(".","-")));
			loanRecord.setCurrRepayAmt(new BigDecimal(loan.getCurrRepayAmt()));
			loanRecord.setStatus(loan.getStatus());
			loanRecord.setActRepayDate(DateUtil.strToDate2(loan.getActRepayDate().replace(".","-")));
			loanRecord.setActRepayAmt(new BigDecimal(loan.getActRepayAmt()));
			loanRecord.setCurrOverdueAmt(new BigDecimal(loan.getCurrOverdueAmt()));
			loanRecord.setCurrOverdueTenor(Integer.getInteger(loan.getCurrOverdueTenor()));
			loanRecord.setTotalOverdueTenor(Integer.getInteger(loan.getTotalOverdueTenor()));
			loanRecord.setMaxOverdueTenor(Integer.getInteger(loan.getMaxOverdueTenor()));
			loanRecord.setOverduePrincipal3160(new BigDecimal(loan.getOverduePrincipal31_60()));
			loanRecord.setOverduePrincipal6190(new BigDecimal(loan.getGetOverduePrincipal61_90()));
			loanRecord.setOverduePrincipal91180(new BigDecimal(loan.getGetOverduePrincipal91_180()));
			loanRecord.setOverduePrincipal180(new BigDecimal(loan.getGetOverduePrincipal180()));
			// TODO 设置贷款表中的查询流水号
			loanRecord.setSerialNum(serialNum);
			loanRecord.setCustIdNumber(certno);
			loanRecord.setLoanId(loanId);
			loanRecord.setCreatedBy("sys");
			loanRecord.setCreatedDate(new Date());
			loanRecord.setUpdatedBy("sys");
			loanRecord.setUpdatedDate(new Date());
			// 贷款信息存库
			loanRecordsMapper.insert(loanRecord);
		}
	}

}
